/**
 * @param {number[]} candidates
 * @param {number} target
 * @return {number[][]}
 */
var combinationSum = function (candidates, target) {
  let res = [],
    path = [],
    sum = 0

  backtrace(0)

  function backtrace(index) {
    if (sum > target) return
    if (sum == target) {
      res.push([...path])
      return 
    }

    for (let i = index; i < candidates.length && sum+candidates[i]<=target; i++) {
      sum+=candidates[i]
      path.push(candidates[i])
      backtrace(i)
      path.pop()
      sum-=candidates[i]

    }
  }

  return res
}

combinationSum([8,7,4,3],11)
